package xuegao.study.mall.web.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.dto.OmsOrderDetail;
import xuegao.study.mall.web.domain.ConfirmOrderResult;
import xuegao.study.mall.web.domain.OrderParam;
import xuegao.study.mall.web.service.PortalOrderService;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;

import java.util.List;
import java.util.Map;

/**
 * app端订单相关api
 */
@RestController
@RequestMapping("/order")
public class OmsPortalOrderController {

    @Autowired
    private PortalOrderService portalOrderService;

    @ApiOperation("根据购物车信息生成确认单")
    @PostMapping("/generateConfirmOrder")
    public CommonResult<ConfirmOrderResult> generateConfirmOrder(@RequestBody List<Long> cartIds) {
        ConfirmOrderResult confirmOrderResult = portalOrderService.generateConfirmOrder(cartIds);
        return CommonResult.success(confirmOrderResult);
    }

    @ApiOperation("根据购物车信息生成订单")
    @PostMapping("/generateOrder")
    public CommonResult generateOrder(@RequestBody OrderParam param) {
        Map<String, Object> result = portalOrderService.generateOrder(param);
        return CommonResult.success(result, "下单成功");
    }

    @ApiOperation("根据id获取订单详情")
    @GetMapping("/detail/{orderId}")
    public CommonResult<OmsOrderDetail> detail(@PathVariable Long orderId) {
        OmsOrderDetail detail = portalOrderService.detail(orderId);
        return CommonResult.success(detail);
    }

    @ApiOperation("用户支付成功的回调")
    @PostMapping("/paySuccess")
    public CommonResult paySuccess(@RequestParam Long orderId, @RequestParam Integer payType) {
        Integer count = portalOrderService.paySuccess(orderId, payType);
        return CommonResult.success(count, "支付成功!");
    }

    @ApiOperation("按状态分页获取用户订单列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<OmsOrderDetail>> list(@RequestParam Integer status, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        CommonPage<OmsOrderDetail> orderPage = portalOrderService.list(status, pageNum, pageSize);
        return CommonResult.success(orderPage);
    }


    @ApiOperation("自动取消超时订单")
    @PostMapping("/cancelTimeOutOrder")
    public CommonResult cancelTimeOutOrder() {
        portalOrderService.cancelTimeOutOrder();
        return CommonResult.success(null);
    }

    @ApiOperation("取消单个超时订单")
    @PostMapping("/cancelOrder")
    public CommonResult cancelOrder(Long orderId) {
        portalOrderService.sendDelayMessageCancelOrder(orderId);
        return CommonResult.success(null);
    }

    @ApiOperation("用户取消订单")
    @PostMapping("/cancelUserOrder")
    public CommonResult cancelUserOrder(Long orderId) {
        portalOrderService.cancelOrder(orderId);
        return CommonResult.success(null);
    }

    @ApiOperation("用户确认收货")
    @PostMapping("/confirmReceiveOrder")
    public CommonResult confirmReceiverOrder(Long orderId) {

        return CommonResult.success(null);
    }

    @ApiOperation("用户删除订单")
    @PostMapping("/deleteOrder")
    public CommonResult deleteOrder(Long orderId) {

        return CommonResult.success(null);
    }
}
